<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Domain_model
 *
 * Model used to retrive domain
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Domain_model extends Base_model {
	function Domain_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Domain_model class loaded' );
	}

	/**
	 * Get Domain
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getDomains( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'domain.id, website.name, domain.url' );
		$this->db->from( "domain" );
		$this->db->join( "website", "domain.website_id = website.id" );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by( 'website.name asc, domain.url asc' ); 
		return $this->db->get( );
	}
	
	/**
	 * Delete Domains
	 *
	 * @access public
	 * @param $website_id
	 */
	function delete( $website_id ) {		
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/admin/domain', 'location' );
		}

		foreach( $selected as $website_id ) {
			$this->db->delete( 'domain', array( 'website_id' => $website_id ) );
		}
	}
	
	/**
	 * Insert new Domains
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//@todo: check if domain entry exists already
		//create new domain entry
		$domains = explode( ",", $data['domains'] );
		foreach( $domains as $key => $value ) {
			$this->db->set( 'website_id',	$data['website_id']	);
			$this->db->set( 'url',			trim( $value) 		);
			$this->db->set( 'language_id',	$data['language']	);
		
			//insert the new domain
			$this->db->insert( 'domain' );
		} //foreach
	}
	
	/**
	 * Constructs an array of the given websites to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildDomainDropdown( $id ) {
		$domains = $this->getDomains( "website.id = '" . $id."'" );
		
		$dropdown = '';
		foreach( $domains->result( ) as $row ) {
	    	 $dropdown .= $row->url . ", ";
		}
		
		return $dropdown;
	}
}
/* End of file domain_model.php */
/* Location: ./modules/articles/models/domain_model.php */